extends Control

# --------------------变量声明----------------------#
# 刷新花费
var cost:int
# 回合结束
var roundOver:=false
@export var stats:Stats

@onready var feature=$Feature
@onready var item=$Feature/Item

@onready var attrList=$Attribute/MarginContainer/ScrollContainer/List
@onready var attrItem=$Attribute/MarginContainer/ScrollContainer/List/Item

@onready var refresh=$Refresh
@onready var title=$Title
@onready var continueBtn=$Continue

const path='res://资源/界面/'
const GROUP={
	'damage':{
		'name':'攻击力',
		'type1':{
			'name':'基础伤害',
			'img':'damage'
		},
		'type2':{
			'name':'基础伤害倍数',
			'img':'damage'
		},
	},
	'speed':{
		'name':'敏捷',
		'type1':{
			'name':'基础移速',
			'img':'speed'
		},
		# 'type2':{
		# 	'name':'射击速度',
		# 	'img':'speed'
		# },
	},
	'hp':{
		'name':'健康',
		'type1':{
			'name':'最大健康值',
			'img':'maxHp'
		}
	},
	'rate':{
		'name':'叠加',
		'type1':{
			'name':'金币获取叠加',
			'img':'gold'
		},
		'type2':{
			'name':'经验获取叠加',
			'img':'exp'
		}
	},
}
const attrData={
	"damage":{
		"group":GROUP.damage,
		"type":'type1',
		"range":[1,5]
	},
	"damageRate":{
		"group":GROUP.damage,
		"type":'type2',
		"range":[2,4]
	},
	"speed":{
		"group":GROUP.speed,
		"type":'type1',
		"range":[50,200]
	},
	"maxHp":{
		"group":GROUP.hp,
		"type":'type1',
		"range":[1,10]
	},
	"goldRate":{
		"group":GROUP.rate,
		"type":'type1',
		"range":[1,5]
	},
	"expRate":{
		"group":GROUP.rate,
		"type":'type2',
		"range":[1,5]
	},
}

# --------------------生命周期----------------------#
func _ready():
	hide()
	item.hide()
	attrItem.hide()
	continueBtn.hide()
	stats.levelUp.connect(updateLevel)
	# 设置语言
	# TranslationServer.set_locale('en')
	pass

func init():
	cost=2
	show()
	renderAttr()

	if stats.addNum>0 :
		title.text='升级了！'
		renerBtn()
		continueBtn.hide()
		renderFeature()
	else:
		title.text='恭喜通过，获得新武器！'
		refresh.hide()
		continueBtn.show()

# --------------------函数调用----------------------#
# 渲染刷新按钮
func renerBtn():
	refresh.show()
	refresh.text='刷新：-'+str(cost)

# 渲染可选特性
func renderFeature():
	feature.show()
	# 清空
	for it in feature.get_children():
		if it.is_visible():
			feature.remove_child(it)
			it.queue_free()
	# 重绘
	var keys=attrData.keys()
	for i in range(4):
		var it=item.duplicate()
		it.show()

		var num=randi_range(0,keys.size()-1)
		var attr=attrData[keys[num]]
		var featureItem=attr.group[attr.type]
		var source=load(path+featureItem.img + '.png')
		it.get_node('Icon/HBoxContainer/TextureRect').texture=source
		it.get_node('Icon/HBoxContainer/VBoxContainer/Name').text=attr.group.name
		
		var val=randi_range(attr.range[0],attr.range[1])
		it.get_node('Desc').text="[color=green]+"+str(val)+"[/color]"+featureItem.name

		it.get_node('Button').pressed.connect(select.bind({
			"key":keys[num],
			"attr":attr,
			"val":val
		}))
		feature.add_child(it)
	pass

# 渲染个人属性
func renderAttr():
	# 清空
	for it in attrList.get_children():
		if it.is_visible():
			attrList.remove_child(it)
			it.queue_free()
	var props=stats.get_script().get_script_property_list()
	# print('props',props)
	for prop in props:
		if prop.name.rfind('.gd')==-1:
			var attr=attrItem.duplicate()
			attr.show()
			attr.get_node('Name').text=tr(prop.name)
			attr.get_node('Value').text=str(stats[prop.name])

			attrList.add_child(attr)
	pass

# 清除地图上的敌人和金币
func clearMap():
	var tree=get_tree()
	var drops=tree.get_nodes_in_group('dropItem')
	var enemys=tree.get_nodes_in_group('enemy')
	for it in drops:
		if it.copy:
			Main.actionNode.remove_child(it)
			it.queue_free()
	for it in enemys:
		if it.copy:
			get_parent().get_parent().get_node('敌众').remove_child(it)
			it.queue_free()
	pass
# --------------------事件信号----------------------#
# 选择特性
func select(param):
	stats[param.key]+=param.val
	stats.addNum-=1
	renderAttr()
	if stats.addNum==0:
		feature.hide()
		refresh.hide()
		continueBtn.show()
	else:
		renderFeature()
	pass

# 刷新特性
func _on_refresh_pressed() -> void:
	if  stats.gold<cost:
		return
	renderFeature()
	stats.gold-=cost
	cost*=2
	renerBtn()
	pass

# 继续游戏
func _on_continue_pressed() -> void:
	hide()
	get_tree().paused=false

	if roundOver:
		clearMap()
		roundOver=false
		get_parent().get_node('倒计时').initRound()
	pass

# 升级 
func updateLevel():
	init()
	get_tree().paused=true
	pass

# 回合结束
func _on_round_end(_round) -> void:
	init()
	roundOver=true
	get_tree().paused=true
	pass 
